﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FamilyJobsUI.DB1;

using System.Data.Linq;
//using Kendo.Mvc.Examples.Models;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace FamilyJobsUI.Controllers
{
    [RequireHttps]
    public class UserPointsController : Controller
    {
        private DB1.FamilyJobsDB db = new DB1.FamilyJobsDB();

        public ActionResult Index_GetData([DataSourceRequest]DataSourceRequest request)
        {
            var userPoints_Index_Rows = getUserPointsList();
            return Json(userPoints_Index_Rows.ToDataSourceResult(request));
            //var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };

            //string json = JsonConvert.SerializeObject(t_UserPoints, Formatting.Indented, serializerSettings);
        }

        public IEnumerable<FamilyJobsUI.Models.UserPoints_Index_RowModel> getUserPointsList()
        {
            var userPoints_Index_Rows =
                db.T_UserPoints
                .Include(t => t.T_PointTypes)
                .Include(t => t.T_Users)
                .OrderByDescending(t => t.up_CreatedOn)
                .Select(a => new FamilyJobsUI.Models.UserPoints_Index_RowModel()
                {
                    up_ID = a.up_ID,
                    up_Points = a.up_Points,
                    up_Reason = a.up_Reason,
                    up_CreatedOn = a.up_CreatedOn,
                    pt_Name = a.T_PointTypes.pt_Name,
                    user_Name = a.T_Users.user_Name
                })
                ;
            return userPoints_Index_Rows;
        }


        // GET: UserPoints
        public ActionResult Index()
        {
            var userPoints_Index_Rows = getUserPointsList();
            var m = new FamilyJobsUI.Models.UserPoints_Index_Model()
            {
                Items = userPoints_Index_Rows
            };
            return View(m);
        }

        // GET: UserPoints/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            T_UserPoints t_UserPoints = db.T_UserPoints.Find(id);
            if (t_UserPoints == null)
            {
                return HttpNotFound();
            }
            return View(t_UserPoints);
        }

        [Authorize]
        // GET: UserPoints/Create
        public ActionResult Create(int? user_ID)
        {
            var users = db.T_Users.Where(a => a.user_PointEligible
                    && (!user_ID.HasValue || a.user_ID == user_ID.Value)
                    );
            var defaultUser = users.First();
            var pointType = db.T_PointTypes.FirstOrDefault();
            var model = new FamilyJobsUI.Models.UserPoints_Create_Model() {
                user_Email = defaultUser.user_Email,
                up_Points = pointType.pt_DefaultPointValue,
                up_PointTypeID = pointType.pt_ID,
                up_UserID = defaultUser.user_ID,
                up_Reason = string.Empty    // no default reasaon
            };

            ViewBag.up_PointTypeID = new SelectList(db.T_PointTypes, "pt_ID", "pt_Name");
            ViewBag.up_UserID = new SelectList(
                db.T_Users.Where(a => a.user_PointEligible)
                , "user_ID", "user_Name");
            return View(model);
        }

        public class Create_GetUserInfo_DataRequest
        {
            public int user_ID { get; set; }
            public int up_PointTypeID { get; set; }
        }
        public class Create_GetUserInfo_DataResponse
        {
            public int user_ID { get; set; }
            public string url { get; set; }
            public int defaultPointValue { get; set;  }
        }

        [HttpPost]
        public ActionResult Create_GetUserInfo(Create_GetUserInfo_DataRequest data)
        {
            var user = db.T_Users.Single(a => a.user_ID ==data.user_ID);

            var pointType = db.T_PointTypes.Single(a => a.pt_ID == data.up_PointTypeID);

            //var srcURL = FamilyJobsUI.Extensions.GravatarHtmlHelper.getImgURL(this.HttpContext, user.user_Email, 200);
            var srcURL = GravatarHelper.GravatarHelper.CreateGravatarUrl(user.user_Email, 200, "", GravatarHelper.GravatarRating.G,true, false);

            var m = new Create_GetUserInfo_DataResponse()
            {
                user_ID = user.user_ID,
                url = srcURL,
                defaultPointValue = pointType.pt_DefaultPointValue
            };
            return Json(m); //, JsonRequestBehavior.AllowGet);

        }

        // POST: UserPoints/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "up_ID,up_Points,up_UserID,up_PointTypeID, up_Reason")] T_UserPoints t_UserPoints)
        {
            if (ModelState.IsValid)
            {
                t_UserPoints.up_CreatedOn = DateTime.Now;
                db.T_UserPoints.Add(t_UserPoints);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.up_PointTypeID = new SelectList(db.T_PointTypes, "pt_ID", "pt_Name", t_UserPoints.up_PointTypeID);
            ViewBag.up_UserID = new SelectList(db.T_Users, "user_ID", "user_Name", t_UserPoints.up_UserID);
            return View(t_UserPoints);
        }

        // GET: UserPoints/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            T_UserPoints t_UserPoints = db.T_UserPoints.Find(id);
            if (t_UserPoints == null)
            {
                return HttpNotFound();
            }
            ViewBag.up_PointTypeID = new SelectList(db.T_PointTypes, "pt_ID", "pt_Name", t_UserPoints.up_PointTypeID);
            ViewBag.up_UserID = new SelectList(db.T_Users, "user_ID", "user_Name", t_UserPoints.up_UserID);
            return View(t_UserPoints);
        }

        // POST: UserPoints/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "up_ID,up_Points,up_UserID,up_PointTypeID")] T_UserPoints t_UserPoints)
        {
            if (ModelState.IsValid)
            {
                db.Entry(t_UserPoints).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.up_PointTypeID = new SelectList(db.T_PointTypes, "pt_ID", "pt_Name", t_UserPoints.up_PointTypeID);
            ViewBag.up_UserID = new SelectList(db.T_Users, "user_ID", "user_Name", t_UserPoints.up_UserID);
            return View(t_UserPoints);
        }

        // GET: UserPoints/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            T_UserPoints t_UserPoints = db.T_UserPoints.Find(id);
            if (t_UserPoints == null)
            {
                return HttpNotFound();
            }
            return View(t_UserPoints);
        }

        // POST: UserPoints/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            T_UserPoints t_UserPoints = db.T_UserPoints.Find(id);
            db.T_UserPoints.Remove(t_UserPoints);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
